#
# $Id$
# $HeadURL$
#
set -e

#!includefile=<common.startup>
common_CONSOLEOUTPUT="Y"

#!includefile=<common.helpers>

##!includefile=<common.paths>
BaseName PROCNAME $0
BaseName SCRIPTNAME $0 .sh
DirName APPPATH $0
normalizePath APPPATH
#DirName APPROOT ${BASH_SOURCE[0]}
APPROOT=$APPPATH
#normalizePath APPROOT
LOGDIR=.
CONFDIR=$APPPATH
TMPDIR=/tmp
LOGFILENAME=""
LCKFILE=$LOGDIR/$SCRIPTNAME.lck
MACHINENAME=`hostname`
PROCDESCR="$PROCNAME($SCRIPTNAME)"

##!includefile=<common.appprops>

if [ -f $APPROOT/params.conf ]; then
  . $APPROOT/params.conf
  ExitIfErr "cannot load $APPROOT/params.conf"
else
usage() {
  echo
  echo "usage $0 [options] args"
  echo
  exit 1
}
fi

if [ -f $APPROOT/$SCRIPTNAME.func ]; then
  . $APPROOT/$SCRIPTNAME.func
  ExitIfErr "cannot execute $SCRIPTNAME.func. exiting."
fi

if [ -f $APPROOT/params.conf ]; then
  . $APPROOT/params.conf
  ExitIfErr "cannot load $APPROOT/params.conf."
fi

if [ -f $APPROOT/$SCRIPTNAME.conf ]; then
  . $APPROOT/$SCRIPTNAME.conf
  ExitIfErr "cannot load $APPROOT/$SCRIPTNAME.conf."
fi


if [ ${#common_opt_var_name[*]} -gt 0 ] && [ ${#common_par_var_name[*]} -gt 0 ]; then

  #!includefile=<common.params>

  common_initParams "$@"
  ExitIfErr "Cannot initialize parameter parser"

  common_COMMANDLINEPARAMS=""
  common_parseParams N "$@"
  ExitIfErr "Cannot parse command line"

  if [ -f $APPPATH/conf/$SCRIPTNAME.defopts ]; then
    eval common_parseParams Y $(<$APPPATH/conf/$SCRIPTNAME.defopts)
    ExitIfErr "Cannot parse default optinos"
  fi

  if [ "$common_HELPONLY" = "Y" ]; then
    common_CONSOLEOUTPUT="Y"
    common_loaded="Y"
    usage
    exit
  fi

  common_checkParams
  ExitIfErr "Cannot check parameters"

fi

#if [ -f $APPPATH/conf/$SCRIPTNAME.options ]; then
##!includefile=<common.params>
#  ExitIfErr "cannot parse parameters"
#fi

common_loaded="Y"
set +e

